const express = require("express");
const message = require("../service/messageService");
const formatResponse = require("../utils/responseFormat");
const messageRouter = express.Router();

//添加留言
messageRouter.post("/", async (req, res, next) => {
  const info = req.body;
  const data = await message.addMessage(info);
  res.send(formatResponse(0, "", data));
});

//删除留言
messageRouter.delete("/:id", async (req, res, next) => {
  const id = req.params.id;
  const data = await message.deleteMessage(id);
  res.send(formatResponse(0, "", data));
});

//分页获取留言
messageRouter.get("/", async (req, res, next) => {
  const page = +req.query.page || 1;
  const limit = +req.query.limit || 10;
  const keyword = req.query.keyword || "";
  const data = await message.getMessageList(page, limit, keyword);
  res.send(formatResponse(0, "", data));
});

module.exports = messageRouter;
